#include <iostream>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;


struct item1{
	int value,weight,num;
};


struct item2{
	int value,weight;
};


int main(){
	int t,m;
	cin>>t>>m;
	item1 a;
	vector<item2> v;
	for(int i=0;i<m;i++){
		cin>>a.num>>a.value>>a.weight;
		item2 l;
		int num=1,sum=0;
		while(sum < a.num){
			l.value = a.value*num;
			l.weight = a.weight*num;
			v.push_back(l);
			sum += num;
			num *= 2;
		}
		num -= sum-a.num;
		l.value = a.value*num;
		l.weight = a.weight*num;
		v[v.size()-1] = l;
	}
	int dp[v.size()+1][t+1];
	for(int i=1;i<=v.size();i++)
		for(int j=1;j<=t;j++)
			dp[i][j] = max(dp[i-1][j],j-v[i-1].weight>=0?dp[i-1][j-v[i-1].weight]+v[i-1].value:0);
	cout<<dp[v.size()][t];
	return 0;
}
